﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Animation;

namespace G.Controls
{


    public class ChildWindow : Window
    {
        //Windows.UI.Xaml.FrameworkElement f;

        private InputPane InputPane;
        protected override void OnOpening()
        {
            base.OnOpening();
            //f = (Windows.UI.Xaml.FrameworkElement)Windows.UI.Xaml.Window.Current.Content;
            //f.IsHitTestVisible = false;
            this.Height = Windows.UI.Xaml.Window.Current.Bounds.Height;
            this.Width = Windows.UI.Xaml.Window.Current.Bounds.Width;
            if (InputPane == null)
            {
                InputPane = InputPane.GetForCurrentView();
            }
            InputPane.Showing += FeedbackView_Showing;
            InputPane.Hiding += FeedbackView_Hiding;
            this.Popup.IsLightDismissEnabled = true;
            Windows.UI.Xaml.Window.Current.SizeChanged += Current_SizeChanged;
        }
        public ChildWindow()
        {
            this.DefaultStyleKey = typeof(ChildWindow);
        }
        void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            this.Height = e.Size.Height;
            this.Width = e.Size.Width;
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            InitialSIP();
        }

        #region 软键盘打开后界面自动上浮功能
        private void InitialSIP()
        {
            FrameworkElement f = this.Content as FrameworkElement;
            if (f != null)
            {
                foreach (FrameworkElement item in f.GetVisualDescendants<FrameworkElement>())
                {
                    if (item is TextBox || item is PasswordBox)
                    {
                        item.GotFocus -= item_GotFocus;
                        item.GotFocus += item_GotFocus;
                    }
                }
            }
        }

        private double InputPaneHeight = 0, MoveHeight = 0;

        void item_GotFocus(object sender, RoutedEventArgs e)
        {
            this.MoveHeight = MovePopupOnFocus(sender as FrameworkElement, this, this.InputPaneHeight, this.MoveHeight);
        }

        private double MovePopupOnFocus(FrameworkElement sender, FrameworkElement parent, double inputPaneHeight, double oldHeightOffset)
        {
            double moveHeight = 0;
            if (inputPaneHeight > 0)
            {
                double windowHeight = Windows.UI.Xaml.Window.Current.Bounds.Height;//窗口大小
                var p = sender.TransformToVisual(parent).TransformPoint(new Windows.Foundation.Point(0, 0));//距离窗体顶部的距离
                moveHeight = sender.ActualHeight + p.Y + 100 + inputPaneHeight - windowHeight;//计算向上偏移量
            }
            TranslateTransform ttf = new TranslateTransform();
            parent.RenderTransform = ttf;
            Storyboard sb = new Storyboard();
            sb.Children.Add(Utils.AnimationHelp.CreateDoubleAnimation(-oldHeightOffset, moveHeight > 0 ? -moveHeight : 0, new Duration(new System.TimeSpan(0, 0, 0, 0, 150)), "Y", ttf));
            sb.Begin();
            return moveHeight > 0 ? moveHeight : 0;
        }
        void FeedbackView_Hiding(Windows.UI.ViewManagement.InputPane sender, Windows.UI.ViewManagement.InputPaneVisibilityEventArgs args)
        {
            this.InputPaneHeight = args.OccludedRect.Height;
            this.MoveHeight = MovePopupOnFocus(this, this, this.InputPaneHeight, this.MoveHeight);
        }

        void FeedbackView_Showing(Windows.UI.ViewManagement.InputPane sender, Windows.UI.ViewManagement.InputPaneVisibilityEventArgs args)
        {
            this.InputPaneHeight = args.OccludedRect.Height;
            this.MoveHeight = MovePopupOnFocus(Windows.UI.Xaml.Input.FocusManager.GetFocusedElement() as FrameworkElement, this, this.InputPaneHeight, this.MoveHeight);

        }

        protected override void OnPointerPressed(Windows.UI.Xaml.Input.PointerRoutedEventArgs e)
        {
            //按下其他地方关闭SIP
            this.Focus(Windows.UI.Xaml.FocusState.Pointer);
            base.OnPointerPressed(e);
        }
        #endregion


        public event EventHandler Closed;

        protected override void OnClosed()
        {
            base.OnClosed();
            if (InputPane == null)
            {
                InputPane = InputPane.GetForCurrentView();
            }
            InputPane.Showing -= FeedbackView_Showing;
            InputPane.Hiding -= FeedbackView_Hiding;
            if (Closed != null)
            {
                Closed(this, EventArgs.Empty);
            }
            //f.IsHitTestVisible = true;
            Windows.UI.Xaml.Window.Current.SizeChanged -= Current_SizeChanged;
        }
    }
}
